package org.jsola.hr.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import org.jsola.core.Page;
import org.jsola.hr.common.ExampleKit;
import org.jsola.hr.constant.HrConstants;
import org.jsola.hr.dao.ISalaryPlanFormulaDAO;
import org.jsola.hr.dto.SalaryFormulaConditionAddDTO;
import org.jsola.hr.dto.SalaryPlanFormulaAddDTO;
import org.jsola.hr.dto.SalaryPlanFormulaUpdateDTO;
import org.jsola.hr.entity.SalaryPlanFormulaDO;
import org.jsola.hr.query.SalaryPlanFormulaQuery;
import org.jsola.hr.service.ISalaryFormulaConditionService;
import org.jsola.hr.service.ISalaryPlanFormulaService;
import org.jsola.hr.vo.SalaryPlanFormulaListVO;
import org.jsola.hr.vo.SalaryPlanFormulaVO;
import org.jsola.user.core.TokenUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author zhr
 */
@Service("hrSalaryPlanFormulaServiceImpl")
public class SalaryPlanFormulaServiceImpl implements ISalaryPlanFormulaService {

    @Autowired
    private ISalaryPlanFormulaDAO salaryPlanFormulaDAO;
    @Autowired
    private ISalaryFormulaConditionService  salaryFormulaConditionService;

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public SalaryPlanFormulaVO save(SalaryPlanFormulaAddDTO salaryPlanFormulaAddDTO, TokenUser tokenUser) {
        // 转DO
        SalaryPlanFormulaDO salaryPlanFormulaDO = salaryPlanFormulaAddDTO.to(SalaryPlanFormulaDO.class);
        // 保存
        salaryPlanFormulaDO = save(salaryPlanFormulaDO, tokenUser.getSiteId(), tokenUser.getUserId());
        return salaryPlanFormulaDO.to(SalaryPlanFormulaVO.class);
    }

    @Override
    public void addList(String salaryPlanId,List<SalaryPlanFormulaAddDTO> salaryPlanFormulaAddDTOList, TokenUser tokenUser) {

        if (CollectionUtil.isEmpty(salaryPlanFormulaAddDTOList)){
            return;
        }
        List<SalaryFormulaConditionAddDTO> list=new ArrayList();
        //循环添加并取得id 赋予子项
        for (SalaryPlanFormulaAddDTO formulaAddDTO:salaryPlanFormulaAddDTOList){
            formulaAddDTO.setSalaryPlanId(salaryPlanId);
           SalaryPlanFormulaVO salaryPlanFormulaVO=   this.save(formulaAddDTO,tokenUser);
           List<SalaryFormulaConditionAddDTO> conditionAddDTOList= formulaAddDTO.getSalaryFormulaConditionAddDTOList();
           String formulaId=salaryPlanFormulaVO.getId();
           if (CollectionUtil.isNotEmpty(conditionAddDTOList)){
               conditionAddDTOList.forEach(c->{
                   c.setFormulaId(formulaId);
                   c.setSalaryPlanId(salaryPlanId);
               });
               list.addAll(conditionAddDTOList);
           }
        }
        salaryFormulaConditionService.batchSave(list,tokenUser);

    }

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public List<SalaryPlanFormulaVO> batchSave(List<SalaryPlanFormulaAddDTO> salaryPlanFormulaAddDTOList, TokenUser tokenUser) {
        if(CollectionUtils.isEmpty(salaryPlanFormulaAddDTOList)) {
            return null;
        }
        List<SalaryPlanFormulaDO> salaryPlanFormulaDOList = salaryPlanFormulaAddDTOList
                .parallelStream()
                .map(salaryPlanFormulaAddDTO -> {
                    SalaryPlanFormulaDO salaryPlanFormulaDO = salaryPlanFormulaAddDTO.to(SalaryPlanFormulaDO.class);
                    salaryPlanFormulaDO.preInsert(tokenUser.getUserId());
                    salaryPlanFormulaDO.setSiteId(tokenUser.getSiteId());
                    return salaryPlanFormulaDO;
                }).collect(Collectors.toList());
        salaryPlanFormulaDAO.insertListAndSetId(salaryPlanFormulaDOList);
        return  salaryPlanFormulaDOList
                .parallelStream()
                .map(salaryPlanFormulaDO -> salaryPlanFormulaDO.to(SalaryPlanFormulaVO.class))
                .collect(Collectors.toList());
    }

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public int update(SalaryPlanFormulaUpdateDTO salaryPlanFormulaUpdateDTO, TokenUser tokenUser) {
        //转DO
        SalaryPlanFormulaDO salaryPlanFormulaDO = salaryPlanFormulaUpdateDTO.to(SalaryPlanFormulaDO.class);
        //根据主键更新，只更新非null值
        return updateByIdSelective(salaryPlanFormulaDO, tokenUser.getSiteId(), tokenUser.getUserId());
    }


    @Override
    public void updateForBusiness(String salaryPlanId,List<SalaryPlanFormulaAddDTO> formulaAddDTOList, TokenUser tokenUser) {

        Example example=new Example(SalaryPlanFormulaDO.class);
        example.and().andEqualTo("salaryPlanId",salaryPlanId);
        salaryPlanFormulaDAO.deleteByExample(example);
        Example formulaConditionExample=new Example(SalaryFormulaConditionAddDTO.class);
        formulaConditionExample.and().andEqualTo("salaryPlanId",salaryPlanId);
        salaryFormulaConditionService.deleteByExample(formulaConditionExample,tokenUser);
        if (CollectionUtil.isEmpty(formulaAddDTOList)){
            return;
        }
            this.addList(salaryPlanId,formulaAddDTOList,tokenUser);
    }

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public int deleteByIds(TokenUser tokenUser, String... salaryPlanFormulaIds) {
        return logicalDeleteByIds(tokenUser.getSiteId(), tokenUser.getUserId(), (Object[]) salaryPlanFormulaIds);
    }


    @Override
    public SalaryPlanFormulaVO selectById(String salaryPlanFormulaId, String siteId) {
        SalaryPlanFormulaDO salaryPlanFormulaDO = selectDOById(salaryPlanFormulaId, siteId);
        if (salaryPlanFormulaDO == null) {
            return null;
        }
        return salaryPlanFormulaDO.to(SalaryPlanFormulaVO.class);
    }

    @Override
    public List<SalaryPlanFormulaListVO> select(SalaryPlanFormulaQuery salaryPlanFormulaQuery, String siteId) {
        List<SalaryPlanFormulaDO> salaryPlanFormulaDOList = selectDO(salaryPlanFormulaQuery, siteId);
        if(CollectionUtils.isEmpty(salaryPlanFormulaDOList)) {
            return salaryPlanFormulaDOList == null ? null : new ArrayList<>();
        }
        return salaryPlanFormulaDOList.stream()
                .map(salaryPlanFormulaDO -> salaryPlanFormulaDO.to(SalaryPlanFormulaListVO.class))
                .collect(Collectors.toList());
    }

    @Override
    public int selectCount(SalaryPlanFormulaQuery salaryPlanFormulaQuery, String siteId) {
        Example example = buildExample(salaryPlanFormulaQuery,siteId);
        return salaryPlanFormulaDAO.selectCountByExample(example);
    }

    @Override
    public Page<SalaryPlanFormulaListVO> selectPage(SalaryPlanFormulaQuery salaryPlanFormulaQuery, String siteId) {
        Example example = buildExample(salaryPlanFormulaQuery,siteId);
        Page<SalaryPlanFormulaDO> page = salaryPlanFormulaDAO.selectPageByExample(example,
                salaryPlanFormulaQuery.getPageNo(),
                salaryPlanFormulaQuery.getPageSize());

        return page.to(SalaryPlanFormulaListVO.class);
    }

    @Override
    public SalaryPlanFormulaDO selectDOById(String salaryPlanFormulaId, String siteId) {
        return listById(salaryPlanFormulaId, siteId);
    }

    @Override
    public List<SalaryPlanFormulaDO> selectDO(SalaryPlanFormulaQuery salaryPlanFormulaQuery, String siteId) {
        Example example = buildExample(salaryPlanFormulaQuery,siteId);
        return salaryPlanFormulaDAO.selectByExample(example);
    }


    /**
     * 根据查询参数，构建example

     * @param salaryPlanFormulaQuery 查询参数
     * @param siteId 所属站点id
     * @return example
     */
    private Example buildExample(SalaryPlanFormulaQuery salaryPlanFormulaQuery, String siteId) {
        Example example = new Example(SalaryPlanFormulaDO.class);
        example.and()
                .andEqualTo("valid", true)
                .andEqualTo("siteId", siteId);
        if (salaryPlanFormulaQuery.getSalaryPlanId() != null) {
            example.and().andEqualTo("salaryPlanId", salaryPlanFormulaQuery.getSalaryPlanId());
        }
        if (salaryPlanFormulaQuery.getFormula() != null) {
            example.and().andEqualTo("formula", salaryPlanFormulaQuery.getFormula());
        }
        if (salaryPlanFormulaQuery.getConditionRelation() != null) {
            example.and().andEqualTo("conditionRelation", salaryPlanFormulaQuery.getConditionRelation());
        }
        if (salaryPlanFormulaQuery.getSort() != null) {
            example.and().andEqualTo("sort", salaryPlanFormulaQuery.getSort());
        }
        if (salaryPlanFormulaQuery.getCompanyId() != null) {
            example.and().andEqualTo("companyId", salaryPlanFormulaQuery.getCompanyId());
        }
        if (salaryPlanFormulaQuery.getGmtCreateStart() != null &&
                salaryPlanFormulaQuery.getGmtCreateEnd() != null) {
            example.and().andBetween("gmtCreate", salaryPlanFormulaQuery.getGmtCreateStart(),
                    salaryPlanFormulaQuery.getGmtCreateEnd());
        }
        if (salaryPlanFormulaQuery.getCreateUserId() != null) {
            example.and().andLike("createUserId", "%" + salaryPlanFormulaQuery.getCreateUserId() + "%");
        }
        if (salaryPlanFormulaQuery.getGmtModifiedStart() != null &&
                salaryPlanFormulaQuery.getGmtModifiedEnd() != null) {
            example.and().andBetween("gmtModified", salaryPlanFormulaQuery.getGmtModifiedStart(),
                    salaryPlanFormulaQuery.getGmtModifiedEnd());
        }
        if (salaryPlanFormulaQuery.getModifiedUserId() != null) {
            example.and().andLike("modifiedUserId", "%" + salaryPlanFormulaQuery.getModifiedUserId() + "%");
        }
        // 排序
        ExampleKit.setExampleOrder(example,salaryPlanFormulaQuery.getOrders());
        return example;
    }
}




